﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Xml.Serialization;

namespace Urbania.GUI.Elements
{
    [Serializable]
    [XmlRoot("PopUp")]
    //[XmlInclude(typeof(GUIElement))]
    public class PopUp : GUIElement
    {
        private Rectangle bounds;
        private Color bg;
        private static Texture2D basicPixel = Utilities.Loader.GetTex("basicPixel");
        private List<GUIElement> elements = new List<GUIElement>();
        private string title;

        public string Title { get { return this.title; } set { this.title = value; } }
        public Color Background { get { return this.bg; } set { this.bg = value; } }
        public Rectangle Bounds { get { return this.bounds; } set { this.bounds = value; } }
        [XmlArray("Elements"), XmlArrayItem(typeof(GUIElement), ElementName = "Element")]
        public List<GUIElement> Elements { get { return this.elements; } set { this.elements = value; } }
        public GUIElement Parent { get { return this.parent; } }

        public PopUp()
        {
            this.bounds = new Rectangle(0, 0, 10, 10);
            this.bg = Color.Beige;
            this.lua = null;
            this.position = new Vector2(bounds.X, bounds.Y);

            
        }

        public PopUp(Rectangle bounds, ref LuaVM lua, GUIElement parent)
        {
            this.bounds = bounds;
            this.position = new Vector2(bounds.X, bounds.Y);
            this.bg = Color.Beige;
            this.lua = lua;
            this.parent = parent;
            this.position = new Vector2(bounds.X, bounds.Y);
        }

        public PopUp(Rectangle bounds, Color bg, ref LuaVM lua, GUIElement parent)
        {
            this.bg = bg;
            this.bounds = bounds;
            this.position = new Vector2(bounds.X, bounds.Y);
            this.lua = lua;
            this.parent = parent;
            this.position = new Vector2(bounds.X, bounds.Y);
        }

        public override void Update(GameTime gameTime)
        {
            foreach (GUIElement e in elements)
                e.Update(gameTime);
        }

        public override void Update(GameTime gameTime, Microsoft.Xna.Framework.Input.MouseState mouse)
        {
            foreach (GUIElement e in elements)
                e.Update(gameTime, mouse);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Rectangle r = new Rectangle((int)(parent.GetPosition().X + bounds.X), (int)(parent.GetPosition().Y + bounds.Y), bounds.Width, bounds.Height);
            spriteBatch.Draw(basicPixel, r, bg);
            foreach (GUIElement e in elements)
                e.Draw(gameTime, spriteBatch);
        }

        public void AddElement(GUIElement e)
        {
            elements.Add(e);
        }

        public void SetParents(ref LuaVM lua)
        {
            foreach (GUIElement e in elements)
            {
                e.SetParent(this);
                e.SetLua(ref lua);
                e.Load();
            }
            this.position = new Vector2(bounds.X, bounds.Y);
        }
    }
}
